function reBindStockEvent(){ $("#store-selector").removeClass("hover"); }

function getIdNameByLevel(level){
    var idName = "";
    if (level == 1) idName = "stock_province_item"; else if (level == 2) idName = "stock_city_item"; else if (level == 3) idName = "stock_area_item";
    return idName;
}


/**新地址列表数据及时间绑定**/
function getAreaList(result,idName,level){
    if (idName && level){
        $("#"+idName).html("");
        var html = ["<ul class='area-list'>"];
        var longhtml = [];
        var longerhtml = [];
        if (result&&result.length > 0){
            for (var i=0,j=result.length;i<j ;i++ ){
                result[i].region_name = result[i].region_name.replace(" ","");
                if(result[i].region_name.length > 12){
                    longerhtml.push("<li class='longer-area'><a href='#none' data-value='"+result[i].region_id+"'>"+result[i].region_name+"</a></li>");
                }else if(result[i].region_name.length > 5){
                    longhtml.push("<li class='long-area'><a href='#none' data-value='"+result[i].region_id+"'>"+result[i].region_name+"</a></li>");
                }else{
                    html.push("<li><a href='#none' data-value='"+result[i].region_id+"'>"+result[i].region_name+"</a></li>");
                }
            }
        }else{
            html.push("<li><a href='#none' data-value='0'> </a></li>");
        }
        html.push(longhtml.join(""));
        html.push(longerhtml.join(""));
        html.push("</ul>");
        $("#"+idName).html(html.join(""));
        $("#"+idName).find("a").click(function(){
            var areaId = $(this).attr("data-value");
            var areaName = $(this).html();
            var level = $(this).parent().parent().parent().attr("data-area");
            JdStockTabs.eq(level).find("a").attr("title",areaName).find("em").html(areaName.length>6?areaName.substring(0,6):areaName);
            level = new Number(level)+1;
            if (level=="2"){
                currentAreaInfo.currentCityId = areaId;
                currentAreaInfo.currentCityName = areaName;
                currentAreaInfo.currentAreaId = 0;
                currentAreaInfo.currentAreaName = "";
            }else if (level=="3"){
                if (requestLevel == 4 && currentAreaInfo.currentAreaId != areaId) requestLevel = 3; 
                currentAreaInfo.currentAreaId = areaId;
                currentAreaInfo.currentAreaName = areaName;
            }
            currentLocation = currentAreaInfo.currentProvinceName;
            GetStockInfoOrNextAreas(currentAreaInfo.currentProvinceId,currentAreaInfo.currentCityId,currentAreaInfo.currentAreaId,level);
        });
        //页面初次加载
        if (currentPageLoad.isLoad){
            var tempDom = $("#"+idName+" a[data-value='"+currentPageLoad.areaCookie[level-1]+"']");
            if (tempDom.length == 0) tempDom = $("#"+idName+" a").eq(0); 
            if(currentPageLoad.areaCookie&&currentPageLoad.areaCookie[level-1]&&currentPageLoad.areaCookie[level-1]>0&&tempDom.length>0){
                tempDom.click();
            }else{
                $("#"+idName+" a:first").click();
            }
        }
    }
}
/**下级地址回调方法**/
function getAreaListcallback(result){
    var level = currentAreaInfo.currentLevel;
    getAreaList(result,getIdNameByLevel(level),level);
}
/**根据父地址及地址等级获取下级地址列表**/
function getChildAreaHtml(region_id,level){
    var idName = getIdNameByLevel(level);
    if (idName){
        $("#stock_province_item,#stock_city_item,#stock_area_item").hide();
        $("#"+idName).show().html("<div class='iloading'>正在加载中，请稍候...</div>");
        JdStockTabs.show().removeClass("curr").eq(level-1).addClass("curr").find("em").html("请选择");
        for (var i=level,j=JdStockTabs.length;i<j ;i++ ){
            JdStockTabs.eq(i).hide();
        }
        currentAreaInfo.currentLevel = level;
		Ajax.call(url_path+'goods.php', "act=regions&region_id="+region_id, getAreaListcallback, 'GET', 'JSON');
    }
}

//获取配送库存信息或下一级地址
function GetStockInfoOrNextAreas(provinceId,cityId,areaId,curLevel){
	try{
		currentAreaInfo.currentProvinceId = provinceId;
		currentAreaInfo.currentCityId = cityId;
		currentAreaInfo.currentAreaId = areaId;
		curLevel = new Number(curLevel);
		if (curLevel == requestLevel){
			currentAreaInfo.currentLevel = curLevel; 
			JdStockTabs.removeClass("curr").eq(curLevel-1).addClass("curr");
			JdStockTabs.find("a").removeClass("hover").eq(curLevel-1).addClass("hover");
			
			reBindStockEvent();
			var address = currentAreaInfo.currentProvinceName+currentAreaInfo.currentCityName+currentAreaInfo.currentAreaName;
			$("#store-selector .text div").html(address).attr("title",address);
			
			if(currentPageLoad.isLoad){
				currentPageLoad.isLoad = false;
			}else{
				$.getJSON(url_path+'index.php?act=set_ipLoc&rg_ipLoc='+provinceId+'-'+cityId+'-'+areaId,{},function(result){
					if(result == provinceId+'-'+cityId+'-'+areaId) window.location.reload()
				});
			}
		}else if (curLevel < requestLevel){ 
			currentAreaInfo.currentLevel = curLevel +1;
			JdStockTabs.removeClass("curr").eq(curLevel).addClass("curr");
			JdStockTabs.find("a").removeClass("hover").eq(curLevel).addClass("hover");
			
			getChildAreaHtml(arguments[curLevel-1],curLevel +1);
		}
	}catch(err){}
}

function getStockInfoByArea(ipLoc){//获取地区库存
    currentPageLoad.areaCookie = ipLoc;
	
    currentAreaInfo.currentProvinceId = ipLoc[0];
	currentAreaInfo.currentProvinceName = iplocation[ipLoc[0]].name;
	currentAreaInfo.currentCityId = ipLoc[1];
	currentAreaInfo.currentAreaId = ipLoc[2];
	
    JdStockTabs.eq(0).find("em").html(currentAreaInfo.currentProvinceName);
    GetStockInfoOrNextAreas(currentAreaInfo.currentProvinceId,currentAreaInfo.currentCityId,currentAreaInfo.currentAreaId,currentAreaInfo.currentLevel);
}

//初始化
var currentPageLoad = {"isLoad":true,"areaCookie":[0,0,0]};
var JdStockTabs = null; 
var JdStockContents = null;
var provinceHtml = '<div data-widget="tabs" class="m JD-stock" id="JD-stock">'
						+'<div class="mt">'
						+'    <ul class="tab">'
						+'        <li data-index="0" data-widget="tab-item" class="curr"><a href="#none" class="hover"><em>请选择</em><i></i></a></li>'
						+'        <li data-index="1" data-widget="tab-item" style="display:none;"><a href="#none" class=""><em>请选择</em><i></i></a></li>'
						+'        <li data-index="2" data-widget="tab-item" style="display:none;"><a href="#none" class=""><em>请选择</em><i></i></a></li>'
						+'    </ul>'
						+'</div>'
						+'<div class="mc" data-area="0" data-widget="tab-content" id="stock_province_item"><ul class="area-list">'+rg_li_str+'</ul></div>'
						+'<div class="mc" data-area="1" data-widget="tab-content" id="stock_city_item"></div>'
						+'<div class="mc" data-area="2" data-widget="tab-content" id="stock_area_item"></div>'
					+'</div>';

var currentLocation = "";
var currentAreaInfo =  {"currentLevel": 1,"currentProvinceId": 0,"currentProvinceName":"","currentCityId": 0,"currentCityName":"","currentAreaId": 0,"currentAreaName":""};
var requestLevel = 3;

$(function(){
    $("#store-selector .content").html(provinceHtml);
    $("#store-selector").mouseover(function(){ $(this).addClass("hover"); });//.mouseout(function(){ $(this).removeClass("hover"); }); 
    JdStockTabs = $("#JD-stock .tab li");
    JdStockContents = $("#JD-stock div[data-widget='tab-content']");
    JdStockTabs.bind('click',function(){
        var level = $(this).attr("data-index"); level = new Number(level);
        JdStockTabs.removeClass("curr").eq(level).addClass("curr");
        JdStockTabs.find("a").removeClass("hover").eq(level).addClass("hover");
        JdStockContents.hide().eq(level).show();
    });
	
    $("#stock_province_item a").click(function() {
        currentPageLoad.isLoad = false;
        try{
            currentAreaInfo.currentProvinceId = $(this).attr("data-value");
            currentAreaInfo.currentProvinceName = currentLocation = $(this).html();
            JdStockTabs.eq(0).find("em").html(currentAreaInfo.currentProvinceName);
            GetStockInfoOrNextAreas(currentAreaInfo.currentProvinceId,0,0,1);
        }catch(err){}
    }).end();
	
    $("#store-selector .close").click(function() { reBindStockEvent(); });
	
	$("#store-selector").mouseout(function(e) {
		if(currentAreaInfo.currentLevel == 3){ reBindStockEvent(); } else { $(this).addClass("hover"); }
	});
});